package arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author wly
 * @version V1.0.0
 * @date 2022/12/23 17:15
 */
public class ArraysSortCustom {
    public static void main(String[] args) {
        int[] arr = {2, 5, 3, 9, 10, -2, 3};
        System.out.println(Arrays.toString(arr));
        ArraysSortCustom.bubble02(arr, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println(Arrays.toString(arr));
    }

    public static void bubble01(int[] arr){
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++){
                if (arr[j] > arr[j + 1]){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

//    结合冒泡，定制排序
    public static void bubble02(int[] arr, Comparator<Integer> c){
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++){
                if (c.compare(arr[j], arr[j + 1]) > 0){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

